﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.common.dbconn;
using EkStudio.com.common.unit;
using EkStudio.Com.UI.Interface;
using System;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace EkStudio.com.ui.form
{
    public partial class ModuleIconSelectForm :XtraForm
    {
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ModuleIconSelectForm));
        private ProjectRootPanel RootPanel = null;
        private ProjectUnitBillInterface UnitPanel = null;
        private BillModuleItem bill = null;

        private string filePath = "";
        private string fileName = "";
        private string ExePath = "";

        public ModuleIconSelectForm(ProjectRootPanel root, ProjectUnitBillInterface unit, BillModuleItem item)
        {
            InitializeComponent();
            this.RootPanel = root;
            this.UnitPanel = unit;
            this.bill = item;
            this.InitForm();
        }


        private void InitForm()
        {
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ExePath = string.Format("{0}Temp", AppDomain.CurrentDomain.BaseDirectory);
            if (!Directory.Exists(ExePath))
            {
                Directory.CreateDirectory(ExePath);
            }
        }
        /// <summary>
        /// 显示图标界面
        /// </summary>
        /// <param name="form"></param>
        /// <returns></returns>
        new public DialogResult ShowDialog(IWin32Window form){
            if (bill.BillModule.Advanced.ModuleIcon != "")
            {
                this.fileName = bill.BillModule.Advanced.ModuleIcon;
                this.filePath = "";
                string tempIcon = string.Format("{0}\\{1}", this.ExePath, bill.BillModule.Advanced.ModuleIcon);
                if (File.Exists(tempIcon))
                {
                    this.ImageShow.Image = Image.FromFile(tempIcon);
                }
                else
                {
                    this.AsynDonwIcon(fileName, tempIcon);
                }
            }
            else
            {
                this.fileName = "";
                this.filePath = "";
                this.ImageShow.Image = null;
            }

            return base.ShowDialog(RootPanel.RootForm);
        }
        /// <summary>
        /// 异步加载图片
        /// </summary>
        private void AsynDonwIcon(string fileName,string filePath)
        {
            RootPanel.Http.FileLoadResources(ProjectUtil.HTTP_FILELOAD_URL, fileName, filePath, HttpConnectUnit.TYPE_MODULE_NODE_IMAGE);
            if (File.Exists(filePath))
            {
                this.ImageShow.Image = Image.FromFile(filePath);
            }
        }

        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnUpLoadImg_Click(object sender, EventArgs e)
        {
            if (ProjectUtil.ICON_LAST_SELECTED_PATH != "")
            {
                this.openFileWin.FileName = ProjectUtil.ICON_LAST_SELECTED_PATH;
            }
            DialogResult DR = this.openFileWin.ShowDialog();
            if (DR == DialogResult.OK)
            {
                Guid guid = Guid.NewGuid();
                string uuid = guid.ToString();
                ProjectUtil.ICON_LAST_SELECTED_PATH = this.openFileWin.FileName;
                filePath = this.openFileWin.FileName;
                fileName = uuid.ToUpper() + Path.GetExtension(filePath);
                this.ImageShow.Image = Image.FromFile(filePath);
                this.BtnSubmit.Enabled = true;
                RootPanel.AddConsoleText(string.Format("文件路径：{0} 文件名{1} 扩展名{2}", filePath, fileName, Path.GetExtension(filePath)), false);
            }
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDeleteImg_Click(object sender, EventArgs e)
        {
            if (fileName != "")
            {
                this.filePath = "";
                this.fileName = "";
                this.ImageShow.Image = null;
                this.BtnSubmit.Enabled = true;
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {

        }

        private void BtnSubmit_Click(object sender, EventArgs e)
        {
            if (UnitPanel.ReadOnly) return;
            if (this.filePath != "")
            {
                bool success = false;
                try
                {
                    success = RootPanel.Http.FileUpLoadResources(ProjectUtil.HTTP_FILEUPLOAD_URL, fileName, filePath, HttpConnectUnit.TYPE_MODULE_NODE_IMAGE);
                }catch(Exception ee)
                {
                    logger.Error("上传文件失败", ee);
                }
                if (success)
                {
                    this.bill.BillModule.Advanced.ModuleIcon = fileName;
                    try
                    {
                        File.Copy(filePath, string.Format("{0}\\{1}", ExePath, fileName), false);
                    }
                    catch (Exception ee)
                    {
                        logger.Error("复制文件失败", ee);
                    }
                    this.UnitPanel.SaveStatus = false;
                }
            }
            else
            {
                if (this.fileName == "")
                {
                    this.bill.BillModule.Advanced.ModuleIcon = "";
                    this.UnitPanel.SaveStatus = false;
                }
            }
            
        }
    }
}